<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Monog extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Tags');
    }
    
    public function get_one_by_slug($slug)
    {
        $field = 'mono.monid,mono.slug,mono.cdtime,mono.mdtime,mono.status,mono.order,mono.uid,mono.titles,mono.description,mono.texts,u.screenName';
        
        $this->db->select($field);
        $this->db->from('monographic mono');
        $this->db->join('users u', 'mono.uid=u.uid', 'left');
        
        $this->db->where(array('mono.slug'=>$slug));
        $monographic = $this->db->get()->row_array();
        if($isPicture = $this->get_picture($monographic['monid']))
            $monographic['pictures'] = $isPicture;
        
        if($isTag = $this->Tags->get_tag_monographic_by_monid($monographic['monid']))
        {
            foreach($isTag as $tg)
            {
                $tgs[$tg['slug']] = $tg['name']; 
            }
            $monographic['tags'] = $tgs;
        }
        return $monographic;
    }

    function get_one($id)
    {
        $param['field'] = 'mono.monid,mono.slug,mono.cdtime,mono.mdtime,mono.status,mono.order,mono.uid,mono.titles,mono.description,mono.texts,u.screenName,m.name';
        
        $this->db->select($param['field']);
        $this->db->from('monographic mono');
        $this->db->join('mono_metas mom', 'mom.monid=mono.monid', 'left');
        $this->db->join('metas m', 'mom.mid=m.mid', 'left');
        $this->db->join('users u', 'mono.uid=u.uid', 'left');
        
        $this->db->where(array('mono.monid'=>$id));
        //$this->db->or_where(array('mono.parentid'=>$id));
        $monographic = $this->db->get()->row_array();
        if($isPicture = $this->get_picture($id))
            $monographic['pictures'] = $isPicture;
        
        if($isTag = $this->Tags->get_tag_monographic_by_monid($id))
        {
            foreach($isTag as $tg)
            {
                $tgs[$tg['slug']] = $tg['name']; 
            }
            $monographic['tags'] = implode(',', $tgs);
        }
        
        return $monographic;
    }
    
    function get_picture($monid){
        $this->load->model('Files_model');
        $picture = $this->Files_model->get_files($monid);
        $returnPicture = array();
        
        if($picture)
        {
            foreach($picture as $pic){
                preg_match('/uploads(.+)/', $pic['full_path'], $full_path);
                $pictures = array(
                    'file_id' => $pic['photo_id'],
                    'name' => $pic['file_name'],
                    'file_path' => base_url().$full_path[0]
                );
                array_push($returnPicture, $pictures);
            }
        }
        
        return $returnPicture;
    }
    
    function getMonographicCount(array $param)
    {
        $this->db->select('mono.monid');
        $this->db->from('monographic mono');
        $this->db->join('mono_metas mom', 'mom.monid=mono.monid', 'left');
        $this->db->join('metas m', 'mom.mid=m.mid', 'left');
        $this->db->join('users u', 'mono.uid=u.uid', 'left');
        if(isset($param['where']) && ! empty($param['where']))
        {
            $this->db->where($param['where']);
        }
        if(isset($param['like']) && ! empty($param['like']))
        {
            $this->db->like($param['like']);
        }
        return $this->db->count_all_results();
    }
    
    function getMonographic(array $param, $page=0, $limit=20)
    {
        if(!isset($param['field']) || empty($param['field']))
        {
            $param['field'] = 'mono.monid,mono.slug,mono.cdtime,mono.mdtime,mono.status,mono.order,mono.uid,mono.titles,mono.description,mono.texts,u.screenName';
        }

        $this->db->select($param['field']);
        $this->db->from('monographic mono');
        $this->db->join('users u', 'mono.uid=u.uid', 'left');
        if(isset($param['where']) && ! empty($param['where']))
        {
            $this->db->where($param['where']);
        }
        if(isset($param['like']) && ! empty($param['like']))
        {
            $this->db->like($param['like']);
        }
        if(!isset($param['order']) || empty($param['order']))
        {
            $param['order']='cdtime desc';
        }
        
        $this->db->order_by($param['order']);
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
    
    function add(array $monographic)
    {
        $this->load->helper('common');
        /** 构建插入结构 */
        $insertStruct = array(
            'slug'          =>  empty($monographic['slug']) ? NULL : slugName($monographic['slug']),
            'cdtime'        =>  time(),
            'mdtime'        =>  time(),
            'status'        =>  empty($monographic['status']) ? 'publish' : $monographic['status'],
            'order'         =>  empty($monographic['order']) ? 0 : intval($monographic['order']),
            'uid'           =>  isset($monographic['uid']) ? $monographic['uid'] : $this->session->userdata('uid'),
            'titles'        =>  empty($monographic['titles']) ? NULL : $monographic['titles'],
            'description'   =>  empty($monographic['description']) ? NULL : $monographic['description'],
            'texts'         =>  empty($monographic['texts']) ? NULL : $monographic['texts'],
        );

        if (!empty($monographic['monid']))
        {
            unset($insertStruct['cdtime']);
            $this->db->where('monid', $monographic['monid']);
            $insertId = $this->db->update('monographic', $insertStruct);
        }
        else
        {
            /** 首先插入部分数据 */
            $this->db->insert('monographic', $insertStruct);
            $insertId = $this->db->insert_id();
        }
        /** 处理附件 */
        if($insertId > 0 && isset($monographic['parent']) && !empty($monographic['parent']))
        {
            $this->load->model('Files_model');
            $this->Files_model->add_monographic_files($insertId, $monographic['parent']);
        }
        
        /** 单独的页面 */
        if($insertId > 0 && !empty($monographic['page']))
        {
            $this->Tags->add_monographic($insertId, $monographic['page'], $monographic['page']);
        }
        
        /** auto tag */
        if($insertId > 0 && !empty($monographic['autotag']))
        {
            $this->Tags->add_monographic($insertId, $monographic['autotag']);
        }
        /** keyword */
        if($insertId > 0 && !empty($monographic['tag']))
        {
            $this->Tags->add_monographic($insertId, $monographic['tag']);
        }
        
        /** 更新缩略名 */
        if ($insertId > 0 && empty($monographic['slug'])) {
            $this->db->where('monid', $insertId);
            $this->db->update('monographic', array('slug'=>$insertId));
        }

        return $insertId;
    }
    
    
    public function deleteMonographic($monid)
    {
        //删除tag关联
        $this->load->model('Tags');
        $this->Tags->deleteRelationships($monid);
        
        /** 删除附件 */
        $this->db->delete('monographic', array('parentid' => $monid));
        
        //删除Monographic
        return $this->db->delete('monographic', array('monid' => $monid));
    }
}